package com.tgy.JUC.Help;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/***
 * @ClassName: SemaphoreTest
 * @Description: Semaphore 测试
 * @Auther: 送你一块二向箔
 * @Date: 15:57 2022/3/11
 * @version : V1.0
 */
public class SemaphoreTest  {
    public static void main(String[] args) {
        run();
    }

    public static void run() {
        /// 线程数量：停车位！限流
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();// 得到
                    System.out.println(Thread.currentThread().getName()+"抢到车位！");
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println(Thread.currentThread().getName()+"离开车位！");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();// 释放
                }
            },i+"号线程").start();
        }
    }
}
